package com.base.myproject.client.ui;


import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BaseObject;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.dialog.MessageDialog;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.store.StoreListener;
import com.extjs.gxt.ui.client.store.Record.RecordUpdate;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;

public class BaseTabItem extends TabItem implements Listener {

	java.util.LinkedList<BaseStore<BaseObject>> basestorell = new java.util.LinkedList<BaseStore<BaseObject>>();
	Widget mainjdbcpanel = null;
	MessageDialog message = new MessageDialog("数据已经发生改变，点“是” 进行保存，点“否” 不进行保存退出,点“取消”不做任何操作。");
	public BaseTabItem() {
		message.setHeading("提示");
		message.setButtons(Dialog.YESNOCANCEL);
		
		this.setScrollMode(Scroll.AUTO);
		this.addListener(Events.Select, new Listener() {
		
			@Override
			public void handleEvent(BaseEvent be) {
				// TODO Auto-generated method stub
				System.out.println("item Select");
				if(mainjdbcpanel instanceof Toolbar)
				{
					((Toolbar)mainjdbcpanel).initToolBar();
				}
				else
				{
					BaseToolBar.getInstance().removeAll();
				}
//				if(mainjdbcpanel!=null)
//					mainjdbcpanel.initToolBar();
			}

		});
		this.addListener(Events.Close,new Listener(){
			public void handleEvent(BaseEvent be) {
			
				GWT.log("--");
				System.out.println("item Close" +BaseTabPanel.getInstance().getItemCount());
				//((Toolbar)mainjdbcpanel).initToolBar();
				if(BaseTabPanel.getInstance().getItemCount()==0)
				{
					BaseToolBar.getInstance().removeAll();
				}
			}

		});
		this.addListener(Events.Close, this);
		this.addListener(Events.BeforeClose, this);
//		this.setLayout(new BorderLayout());
		this.setLayout(new FitLayout());
		
        message.getButtonById(Dialog.NO).addSelectionListener(new SelectionListener<ButtonEvent>(){

				@Override
				public void componentSelected(ButtonEvent ce) {
					sync++;
					close();
				}
			 });
        
        message.getButtonById(Dialog.YES).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				sync++;
				panel.save();
				close();
			}
		 });
        
        message.getButtonById(Dialog.CANCEL).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				message.hide();
			}
		 });
        

	}
	
	BaseJDBCPanel panel;
	
	StoreListener sl = new StoreListener()
	{
		public void storeAdd(StoreEvent se) {
			System.out.println("---add in tabitem");
		}

		public void storeRemove(StoreEvent se) {
			System.out.println("---storeRemove in tabitem");
		}

		public void storeClear(StoreEvent se) {
			System.out.println("---storeClear in tabitem");
		}

		public void storeDataChanged(StoreEvent se) {
			System.out.println("---storeDataChanged in tabitem");
		}
		  public void storeBeforeDataChanged(StoreEvent se) {
			  System.out.println("---storeBeforeDataChanged in tabitem");
		  }
	};
	public boolean add(Widget widget) {

		if (widget instanceof BaseJDBCPanel) {
			panel = (BaseJDBCPanel)widget;
			setBasestorell(((BaseJDBCPanel) widget).getBasestorell());
			((BaseJDBCPanel) widget).initToolBar();
	
		}
		else if(widget instanceof Toolbar)
		{
			//setBasestorell(((Toolbar) widget).getBasestorell());
			((Toolbar) widget).initToolBar();
		}
		mainjdbcpanel = widget;
	
		addStoreListener();
		 BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);

		 
		 centerData.setMargins(new Margins(0, 0, 0, 0));  
		//return super.add(widget,centerData);
		 return super.add(widget);
	}

	/*
	 * 
	 * ÿ��tab��������ڵ�basestore����������Ű�ť��״̬
	 */
	
	@SuppressWarnings("unchecked")
	private void addStoreListener()
	{
		for(int i=0;i<basestorell.size();i++)
		{
			
			basestorell.get(i).addStoreListener(sl);
			basestorell.get(i).addListener(Store.Update, new Listener(){

				@Override
				public void handleEvent(BaseEvent be) {
					// TODO Auto-generated method stub
					
					if(be instanceof StoreEvent)
					{
						
						if(((StoreEvent)be).getOperation().equals(RecordUpdate.EDIT))
						{
							
							BaseToolBar.getInstance().getItem(0).enable();
							
						}
						else if(((StoreEvent)be).getOperation().equals(RecordUpdate.REJECT))
						{
							
							BaseToolBar.getInstance().getItem(0).disable();
						}
						else if(((StoreEvent)be).getOperation().equals(RecordUpdate.COMMIT))
						{
							
							BaseToolBar.getInstance().getItem(0).disable();
						}
					}
				}
				
				
			});
		}
	}
	public java.util.LinkedList<BaseStore<BaseObject>> getBasestorell() {
		return basestorell;
	}

	public void setBasestorell(java.util.LinkedList<BaseStore<BaseObject>> ll) {

		this.basestorell = ll;
	}

	public void registerStore(BaseStore bs) {
		if(basestorell==null)
		 basestorell = new java.util.LinkedList<BaseStore<BaseObject>>();
		if (!basestorell.contains(bs))
			basestorell.add(bs);
	}

	public boolean removeStore(BaseStore bs) {
		
		return basestorell.remove(bs);
	}
	
	public void removeAllStore() {
		basestorell.clear();
	}

	public int getRegisterSize() {
		return basestorell.size();
	}
	int sync =1;
	@Override
	public void handleEvent(final BaseEvent be) {
		Debug.printf(be.getType(),4);
		Debug.printf(Events.Close,4);
		
	//	Events.BeforeClose
		if(be.getType().equals(/*Events.Close*/Events.BeforeClose))
		{
			Debug.printf(mainjdbcpanel.getClass(),	4);
			if(mainjdbcpanel instanceof BaseJDBCPanel)
			{
				Debug.printf("mainjdbcpanel instanceof BaseJDBCPanel",	4);
				for (BaseStore bs:  ((BaseJDBCPanel) mainjdbcpanel).getBasestorell())
				{
					if(bs.isIschanged())
					{
						/*MessageDialog message = new MessageDialog("数据已被修改，需要保存？");
						message.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>(){

								@Override
								public void componentSelected(ButtonEvent ce) {
									
									
									((BaseJDBCPanel) mainjdbcpanel).getMainstore().trySave();
								}
								 
								 
							 });
						message.show();*/
					/*	  Listener<MessageBoxEvent> l = new Listener<MessageBoxEvent>() {  
						      public void handleEvent(MessageBoxEvent ce) {  
						        Button btn = ce.getButtonClicked();  
						      if(btn.getText().equals(GXT.MESSAGES.messageBox_yes()))
						      {
						    	  MessageBox.alert("", "", null);
						    	  be.setCancelled(true);
						      }
						     
						     
						      }  
						    };  
						 MessageBox box = new MessageBox();  
					        box.setButtons(MessageBox.YESNO);  
					        box.setIcon(MessageBox.QUESTION);  
					        box.setTitle("保存？");  
					        box.addCallback(l);  
					        box.setMessage("数据已经发生改变，点“"+Dialog.OK+"” 返回界面进行保存，点“"+Dialog.NO+"” 不进行保存退出。");  
					        box.show();  
					        */
						if(sync==1)
						{
							be.setCancelled(true);
					
						
							message.show();
						}	
							
					}
				}
			}
		}
	/*	else if(be.getType().equals(Events.Close))
		{
			MessageBox.alert("2", "", null);
			//be.setCancelled(true);
		}*/
		
	}
}
